/*
Copyright (c) 2011, Nimar S. Arora
All rights reserved.
*/
#include <math.h>

#include "network.h"
#include "statistics.h"

void stat_PE_init(Stat_PE_t * p_stat, const Graph_t * p_graph)
{
  p_stat->stat_cnt = 0;
  p_stat->stat_sum_inv_like = 0;
}

void stat_PE_update(Stat_PE_t * p_stat, const Graph_t * p_graph,
                    const Instance_t *p_inst)
{
  int f;
  double like;  

  like = 1.0;
  
  for (f=0; f<p_graph->graph_num_facs; f++)
  {
    Factor_t * p_fac;
    
    if (!p_inst->inst_p_is_obs[f])
      continue;

    p_fac = p_graph->graph_p_facs + f;
    
    like *= exp(graph_factor(p_fac, p_inst->inst_p_val));
  }
  
  p_stat->stat_cnt ++;
  p_stat->stat_sum_inv_like += 1.0 / like;
}

void stat_PE_write(const Stat_PE_t * p_stat, const Graph_t * p_graph,
                   FILE * fp)
{
  fprintf(fp, "PE %lf\n", p_stat->stat_cnt / p_stat->stat_sum_inv_like);
}


